package data_structure;

import java.util.LinkedHashMap;
import java.util.TreeMap;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/17 07:31
 */
public class QuickSort2 {
    public static void main(String[] args) {
        TreeMap<Integer,Integer> treeSet = new TreeMap<>();
        treeSet.put(1,1);
        treeSet.put(2,1);
        treeSet.put(3,1);
    }

    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return;
        }
        int pivot = partition(arr, startIndex, endIndex);

        quickSort(arr, startIndex, pivot - 1);
        quickSort(arr, pivot + 1, endIndex);
    }

    public static int partition(int[] arr, int startIndex, int endIndex) {
        int pivot = arr[startIndex];
        int left = startIndex;
        int right = endIndex;
        while (left != right) {
            while (left < right && arr[right] >= pivot) {
                right--;
            }
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            if (left < right) {
                int p = arr[left];
                arr[left] = arr[right];
                arr[right] = p;
            }
        }
        arr[startIndex] = arr[left];
        arr[left] = pivot;
        return left;
    }
}
